package  
{
	import org.flixel.*;
	
	/**
	 * ...
	 * @author Jay Wagner
	 */
	public class LevelSelectState extends FlxState 
	{
		private const TEXTWIDTH:int = 128;
		private const TEXTHEIGHT:int = 25;
		private const IMGWIDTH:uint = 640 / 4;
		private const IMGHEIGHT:uint = 480 / 5;
		
		private var _selectedLevel:uint = 0;
		public var selectedText:FlxText;
		public var selectedText2:FlxText;
		private var _textWidth:uint = 300;
		
		private const _buttonGlowBuffer:uint = 5;
		
		private var _mainMenuButtonBackground:FlxSprite;
		private var _mainMenuButton:FlxButtonPlus;
		private var _menuText1:FlxText;
		private var _menuText2:FlxText;
		private var _level0Sprite:FlxSprite;
		private var _level1Sprite:FlxSprite;
		private var _level2Sprite:FlxSprite;
		private var _level3Sprite:FlxSprite;
		private var _level4Sprite:FlxSprite;
		private var _level0ButtonGlow:FlxSprite;
		private var _level1ButtonGlow:FlxSprite;
		private var _level2ButtonGlow:FlxSprite;
		private var _level3ButtonGlow:FlxSprite;
		private var _level4ButtonGlow:FlxSprite;
		private var _level0ButtonShadow:FlxSprite;
		private var _level1ButtonShadow:FlxSprite;
		private var _level2ButtonShadow:FlxSprite;
		private var _level3ButtonShadow:FlxSprite;
		private var _level4ButtonShadow:FlxSprite;
		private var _lastMousedOver:uint = 5;
		private var _level0Button:FlxButtonPlus;
		private var _level1Button:FlxButtonPlus;
		private var _level2Button:FlxButtonPlus;
		private var _level3Button:FlxButtonPlus;
		private var _level4Button:FlxButtonPlus;
		private var _level0Text:FlxText;
		private var _level1Text:FlxText;
		private var _level2Text:FlxText;
		private var _level3Text:FlxText;
		private var _level4Text:FlxText;
		private var _level0Coin:FlxSprite;
		private var _level1Lock:FlxSprite;
		private var _level2Lock:FlxSprite;
		private var _level3Lock:FlxSprite;
		private var _level4Lock:FlxSprite;
		
		private var _fadeInTimer:Number = 0.0;
		private var _fadeOutTimer:Number = 0.0;
		private const _fadeInMaxTime:Number = 1.0;
		private const _fadeOutMaxTime:Number = 1.0;
		private var _buttonPressed:Boolean = false;
		private var _toWhere:uint;
		
		private const TOTUTORIAL:uint = 0;
		private const TOLEVEL1:uint = 1;
		private const TOLEVEL2:uint = 2;
		private const TOLEVEL3:uint = 3;
		private const TOLEVEL4:uint = 4;
		private const TOMAINMENU:uint = 5;
		
		private var _fadeSprite:FlxSprite;
		
		public function LevelSelectState(fromMainMenu:Boolean = false) 
		{
			var background:FlxSprite = new FlxSprite(0, 0, ImageFiles.menuBackground);
			add(background);
			
			var levelSelectSign:FlxSprite = new FlxSprite(0, 0, ImageFiles.levelSelectSign);
			add(levelSelectSign);
			
			_mainMenuButtonBackground = new FlxSprite(0, FlxG.height, ImageFiles.menuLeftButton);
			_mainMenuButtonBackground.y -= _mainMenuButtonBackground.height;
			add(_mainMenuButtonBackground);
			
			var bgLight:FlxSprite = new FlxSprite(0, 0, ImageFiles.menuBackgroundLight);
			bgLight.blend = "multiply";
			add(bgLight);
			
			_mainMenuButton = new FlxButtonPlus(0, FlxG.height - 60, goToMainMenu);
			_menuText1 = new FlxText(0, 13, 200, "Back").setFormat("FightingSpirit", 30, 0x729954, "center");
			_menuText2 = new FlxText(_menuText1.x, _menuText1.y, _menuText1.width, _menuText1.text).setFormat("FightingSpirit", 36, 0xd8eba2, "center");
			var menuButtonSprite:FlxSprite = new FlxSprite().createGraphic(200, 60, 0x003a5c39);
			var menuButtonHighlightSprite:FlxSprite = new FlxSprite().createGraphic(200, 60, 0x00729954);
			_mainMenuButton.loadGraphic(menuButtonSprite, menuButtonHighlightSprite);
			_mainMenuButton.setMouseOverCallback(mouseOverSound);
			_mainMenuButton.loadText(_menuText1, _menuText2);
			add(_mainMenuButton);
			
			var levelSelectText:FlxText = new FlxText(80, 35, FlxG.width, "Level Select");
			levelSelectText.setFormat("FightingSpirit", 40, 0xffff00, "center", 0xFF000000);
			add(levelSelectText);
			
			selectedText = new FlxText(FlxG.width - 80 - _textWidth, FlxG.height - 40, _textWidth);
			selectedText.setFormat("PowellAntique", 20, 0xffffff, "right", 0xff000000);
			add(selectedText);
			selectedText2 = new FlxText(selectedText.x + _textWidth, selectedText.y, _textWidth)
			selectedText2.setFormat("PowellAntique", 20, 0xffffff, "left", 0xff000000);
			add(selectedText2);
			
			_level0Sprite = new FlxSprite(0, 0, ImageFiles.level00);
			_level0Button = new FlxButtonPlus(IMGWIDTH * 0.5 - 10, IMGHEIGHT * 1.5 + 49, playSelectedLevel, [0]);
			_level0Button.loadGraphic(_level0Sprite);
			_level0Button.setMouseOverCallback(mouseOverLevel, [0]);
			_level0Text = new FlxText(_level0Button.x, _level0Button.y - TEXTHEIGHT - 4, TEXTWIDTH, "Tutorial").setFormat("PowellAntique", 18, 0xFFFFFF, "center", 0xFF000000);
			_level0ButtonShadow = new FlxSprite(_level0Button.x + 2, _level0Button.y + 2).createGraphic(_level0Button.width, _level0Button.height, 0x44000000);
			_level0ButtonGlow = new FlxSprite(_level0Button.x - _buttonGlowBuffer, _level0Button.y - _buttonGlowBuffer);
			_level0ButtonGlow.loadGraphic(ImageFiles.levelHighlight);
			_level0ButtonGlow.visible = false;
			add(_level0ButtonShadow);
			add(_level0ButtonGlow);
			add(_level0Button);
			add(_level0Text);
			
			_level1Sprite = new FlxSprite(0, 0, ImageFiles.level01);
			_level1Button = new FlxButtonPlus(IMGWIDTH * 1.5, IMGHEIGHT * 1 + 40, playSelectedLevel, [1]);
			_level1Button.loadGraphic(_level1Sprite);
			_level1Button.setMouseOverCallback(mouseOverLevel, [1]);
			_level1Text = new FlxText(_level1Button.x, _level1Button.y - TEXTHEIGHT, TEXTWIDTH, "1").setFormat("PowellAntique", 15, 0xFFFFFF, "center", 0xFF000000);
			_level1ButtonShadow = new FlxSprite(_level1Button.x + 2, _level1Button.y + 2).createGraphic(_level1Button.width, _level1Button.height, 0x44000000);
			_level1ButtonGlow = new FlxSprite(_level1Button.x - _buttonGlowBuffer, _level1Button.y - _buttonGlowBuffer);
			_level1ButtonGlow.loadGraphic(ImageFiles.levelHighlight);
			_level1ButtonGlow.visible = false;
			add(_level1ButtonShadow);
			add(_level1ButtonGlow);
			add(_level1Button);
			add(_level1Text);
			
			_level2Sprite = new FlxSprite(0, 0, ImageFiles.level02);
			_level2Button = new FlxButtonPlus(IMGWIDTH * 2.5 + 20, IMGHEIGHT * 1 + 40, playSelectedLevel, [2]);
			_level2Button.loadGraphic(_level2Sprite);
			_level2Button.setMouseOverCallback(mouseOverLevel, [2]);
			_level2Text = new FlxText(_level2Button.x, _level2Button.y - TEXTHEIGHT, TEXTWIDTH, "2").setFormat("PowellAntique", 15, 0xFFFFFF, "center", 0xFF000000);
			_level2ButtonShadow = new FlxSprite(_level2Button.x + 2, _level2Button.y + 2).createGraphic(_level2Button.width, _level2Button.height, 0x44000000);
			_level2ButtonGlow = new FlxSprite(_level2Button.x - _buttonGlowBuffer, _level2Button.y - _buttonGlowBuffer);
			_level2ButtonGlow.loadGraphic(ImageFiles.levelHighlight);
			_level2ButtonGlow.visible = false;
			add(_level2ButtonShadow);
			add(_level2ButtonGlow);
			add(_level2Button);
			add(_level2Text);
			
			_level3Sprite = new FlxSprite(0, 0, ImageFiles.level03);
			_level3Button = new FlxButtonPlus(IMGWIDTH * 1.5, IMGHEIGHT * 2 + 60 + TEXTHEIGHT, playSelectedLevel, [3]);
			_level3Button.loadGraphic(_level3Sprite);
			_level3Button.setMouseOverCallback(mouseOverLevel, [3]);
			_level3Text = new FlxText(_level3Button.x, _level3Button.y - TEXTHEIGHT, TEXTWIDTH, "3").setFormat("PowellAntique", 15, 0xFFFFFF, "center", 0xFF000000);
			_level3ButtonShadow = new FlxSprite(_level3Button.x + 2, _level3Button.y + 2).createGraphic(_level3Button.width, _level3Button.height, 0x44000000);
			_level3ButtonGlow = new FlxSprite(_level3Button.x - _buttonGlowBuffer, _level3Button.y - _buttonGlowBuffer);
			_level3ButtonGlow.loadGraphic(ImageFiles.levelHighlight);
			_level3ButtonGlow.visible = false;
			add(_level3ButtonShadow);
			add(_level3ButtonGlow);
			add(_level3Button);
			add(_level3Text);
			
			_level4Sprite = new FlxSprite(0, 0, ImageFiles.level04);
			_level4Button = new FlxButtonPlus(IMGWIDTH * 2.5 + 20, IMGHEIGHT * 2 + 60 + TEXTHEIGHT, playSelectedLevel, [4]);
			_level4Button.loadGraphic(_level4Sprite);
			_level4Button.setMouseOverCallback(mouseOverLevel, [4]);
			_level4Text = new FlxText(_level4Button.x, _level4Button.y - TEXTHEIGHT, TEXTWIDTH, "4").setFormat("PowellAntique", 15, 0xFFFFFF, "center", 0xFF000000);
			_level4ButtonShadow = new FlxSprite(_level4Button.x + 2, _level4Button.y + 2).createGraphic(_level4Button.width, _level4Button.height, 0x44000000);
			_level4ButtonGlow = new FlxSprite(_level4Button.x - _buttonGlowBuffer, _level4Button.y - _buttonGlowBuffer);
			_level4ButtonGlow.loadGraphic(ImageFiles.levelHighlight);
			_level4ButtonGlow.visible = false;
			add(_level4ButtonShadow);
			add(_level4ButtonGlow);
			add(_level4Button);
			add(_level4Text);
			
			_level0Coin = new FlxSprite(_level0Button.x, _level0Button.y);
			LoadMedalForLevel(_level0Coin, 0);
			add(_level0Coin);
			
			_level1Lock = new FlxSprite(_level1Button.x, _level1Button.y);
			LoadMedalForLevel(_level1Lock, 1);
			add(_level1Lock);
			
			_level2Lock = new FlxSprite(_level2Button.x, _level2Button.y);
			LoadMedalForLevel(_level2Lock, 2);
			add(_level2Lock);
			
			_level3Lock = new FlxSprite(_level3Button.x, _level3Button.y);
			LoadMedalForLevel(_level3Lock, 3);
			add(_level3Lock);
			
			_level4Lock = new FlxSprite(_level4Button.x, _level4Button.y);
			LoadMedalForLevel(_level4Lock, 4);
			add(_level4Lock);
			
			_fadeSprite = new FlxSprite().createGraphic(640, 480, 0xff000000);
			_fadeSprite.alpha = 1;
			add(_fadeSprite);
			
			if (!fromMainMenu)
			{
				FlxG.playMusic(SoundFiles.themeMusic);
			}
		}
		
		private function LoadMedalForLevel(medal:FlxSprite, level:uint):void
		{
			var medalEarned:uint = PlayState.calculateRating(FlxG.scores[level], level);
			switch (medalEarned)
			{
			case 0:
				medal.visible = false;
				break;
			case 1:
				medal.visible = true;
				medal.loadGraphic(ImageFiles.coinBronze);
				break;
			case 2:
				medal.visible = true;
				medal.loadGraphic(ImageFiles.coinSilver);
				break;
			case 3:
				medal.visible = true;
				medal.loadGraphic(ImageFiles.coinGold);
				break;
			}
		}
		
		override public function update():void 
		{
			if (_fadeInTimer < _fadeInMaxTime && !_buttonPressed)
			{
				_fadeInTimer += FlxG.elapsed;
				
				_fadeSprite.alpha = Math.abs(1 - (_fadeInTimer / _fadeInMaxTime));
			}
			
			if (_buttonPressed)
			{
				fadeOut();
			}
			
			if (FlxG.scores[0] < 1)
			{
				_level1Lock.loadGraphic(ImageFiles.levelLock);
				_level1Lock.visible = true;
			}
			if (FlxG.scores[1] < 1)
			{
				_level2Lock.loadGraphic(ImageFiles.levelLock);
				_level2Lock.visible = true;
			}
			if (FlxG.scores[2] < 1)
			{
				_level3Lock.loadGraphic(ImageFiles.levelLock);
				_level3Lock.visible = true;
			}
			if (FlxG.scores[3] < 1)
			{
				_level4Lock.loadGraphic(ImageFiles.levelLock);
				_level4Lock.visible = true;
			}
			
			super.update();
		}
		
		private function fadeOut():void
		{
			_fadeOutTimer = _fadeSprite.alpha / _fadeOutMaxTime;
			_fadeOutTimer += FlxG.elapsed;
			
			if (_fadeOutTimer >= _fadeOutMaxTime)
			{
				switch (_toWhere)
				{
				case TOTUTORIAL:
					FlxG.state = new Level0State();
					break;
				case TOLEVEL1:
					FlxG.state = new Level1State();
					break;
				case TOLEVEL2:
					FlxG.state = new Level2State();
					break;
				case TOLEVEL3:
					FlxG.state = new Level3State();
					break;
				case TOLEVEL4:
					FlxG.state = new Level4State();
					break;
				case TOMAINMENU:
					FlxG.state = new MainMenuState(false, false);
					break;
				}
			}
			
			if ( _fadeSprite.alpha < _fadeOutTimer / _fadeOutMaxTime)
			{
				_fadeSprite.alpha = _fadeOutTimer / _fadeOutMaxTime;
			}
		}
		
		public function goToMainMenu():void
		{
			if (!_buttonPressed && _fadeSprite.alpha < .80)
			{
				FlxG.play(SoundFiles.selectSound);
				_buttonPressed = true;
				_toWhere = TOMAINMENU;
			}
		}
		
		private function mouseOverSound():void
		{
			FlxG.play(SoundFiles.mouseOverSound, 0.75);
		}
		
		private function mouseOverLevel(levelNumber:uint):void
		{
			mouseOverSound();
			
			if (levelNumber == 0)
			{
				selectedText.text = "Tutorial ";
			}
			else
			{
				selectedText.text = "Level " + levelNumber.toString() + " ";
			}
			
			selectedText.text += "High Score: ";
			selectedText2.text = FlxG.scores[levelNumber];
			
			_level0ButtonGlow.visible = false;
			_level1ButtonGlow.visible = false;
			_level2ButtonGlow.visible = false;
			_level3ButtonGlow.visible = false;
			_level4ButtonGlow.visible = false;
			
			_lastMousedOver = levelNumber;
			
			switch (_lastMousedOver)
			{
			case 0:
				_level0ButtonGlow.visible = true;
				break;
			case 1:
				_level1ButtonGlow.visible = true;
				break;
			case 2:
				_level2ButtonGlow.visible = true;
				break;
			case 3:
				_level3ButtonGlow.visible = true;
				break;
			case 4:
				_level4ButtonGlow.visible = true;
				break;
			}
		}
		
		private function playSelectedLevel(levelNumber:uint):void
		{
			if (levelNumber == 0 || FlxG.scores[levelNumber - 1] > 0) // BALEETED!
			{															
				if (!_buttonPressed && _fadeSprite.alpha < .80)									
				{														
					FlxG.music.fadeOut(1.0);
					FlxG.play(SoundFiles.selectSound);
					_buttonPressed = true;
					FlxG.level = levelNumber;
					switch (levelNumber)
					{
						case 0:
						_toWhere = TOTUTORIAL;
						break;
						case 1:
						_toWhere = TOLEVEL1;
						break;
						case 2:
						_toWhere = TOLEVEL2;
						break;
						case 3:
						_toWhere = TOLEVEL3;
						break;
						case 4:
						_toWhere = TOLEVEL4;
						break;
					}
				}
			}
		}
		
	}

}